﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace Classes
{

    public class RetornaDados
    {
        String sSQL = "";

        
        //Properties.Settings.Default["ConnectionString"]

        /* Variaveis de Componente de DB */
        public SqlConnection SqlCon = new SqlConnection("Data Source=localhost;Initial Catalog=FIRSTTOPSavio;Persist Security Info=True;User ID=sa;Password=novalis13579");//System.Configuration.ConfigurationSettings.AppSettings["SivModeloConnectionString"].ToString());
        //public SqlConnection SqlCon = new SqlConnection("Data Source=localhost;Initial Catalog=FIRSTTOPSavio;Persist Security Info=True;User ID=sa;Password=novalis13579");//System.Configuration.ConfigurationSettings.AppSettings["SivModeloConnectionString"].ToString());
        //public SqlCeConnection SqlCon = new SqlCeConnection("Data Source=|DataDirectory|SIVModelo.sdf");//new SqlConnection("Data Source=localhost;Initial Catalog=SivModelo;Persist Security Info=True;User ID=sa;Password=doom");//System.Configuration.ConfigurationSettings.AppSettings["SivModeloConnectionString"].ToString());
        public SqlCommand cmd;
        public DataSet ds;
        public DataTable dataTable;
        /* FIM - Variaveis de Componente de DB */


        /* processo de pesquisa simples */
        public DataTable pesquisar(string pSQL) 
        {
            /* Verifica o comando */
            if (pSQL != "")
                sSQL = pSQL;

            ds = new DataSet();

            try
            {
                
                if (SqlCon.State == ConnectionState.Closed)
                    SqlCon.Open();
                cmd = new SqlCommand(sSQL, SqlCon);
                ds = new DataSet();

                /* Executa a query */
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                dataTable = new DataTable();

                /* faz o retorno dentro do dataset */
                da.Fill(dataTable);
                da.Fill(ds);
                
                da.Dispose();
                return dataTable;
            }
            catch (Exception e)
            {
                MessageBox.Show("Erro ao pesquisar dados.\r\nErro-> "+e.Message, "Erro",
                                 MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return null;
            }
        }

    }
}
